package com.betterjr.modules.account.dubbo;

import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.UserUtils;
import com.betterjr.common.web.AjaxObject;
import com.betterjr.modules.account.dubbo.interfaces.ICustInfoService;
import com.betterjr.modules.account.entity.CustInfo;
import com.betterjr.modules.account.service.CustAccountService;
import com.betterjr.modules.account.service.CustAndOperatorRelaService;

/**
 * 机构基本信息
 *
 * @author liuwl
 *
 */
@Service(interfaceClass = ICustInfoService.class)
public class CustInfoDubboService implements ICustInfoService {
    private static Logger logger = LoggerFactory.getLogger(CustInfoDubboService.class);

    @Autowired
    private CustAndOperatorRelaService custOperatorRelaService;

    @Autowired
    private CustAccountService custAccountService;

    @Override
    public String webQueryCustInfo() {
        final Collection<CustInfo> custInfos = queryCustInfo();
        return AjaxObject.newOk("查询操作员所有的公司列表成功", custInfos).toJson();
    }

    /**
     * 客户查询，根据类型查找            
     * @return 
     * @see com.betterjr.modules.account.dubbo.interfaces.ICustInfoService#webQueryCustInfo() 
     * @author chenlq
     * @date 2018年7月3日 下午5:41:40
     */
    @Override
    public String webQueryCustInfo(String custType) {
        final Collection<CustInfo> custInfos = custAccountService.queryCustInfo(custType);
        return AjaxObject.newOk("客户查询成功！", custInfos).toJson();
    }

    /**
     * 核心企业查询资金方 
     * @param custType
     * @return 
     * @see com.betterjr.modules.account.dubbo.interfaces.ICustInfoService#platformQueryAllCustInfo(java.lang.String) 
     * @author chenlq
     * @date 2018年7月5日 下午3:48:10
     */
    @Override
    public String webCoreEnterpriseQueryMoneyEnterperise(String custType) {
        final Collection<CustInfo> custInfos = custAccountService.coreQueryMoney(custType,
                UserUtils.getOperatorInfo().getId(), UserUtils.getOperatorInfo().getOperOrg());
        return AjaxObject.newOk("资金方客户查询成功！", custInfos).toJson();
    }

    /**
     * 
     * <p>前置查询所有资金企业</p>  
     * <p>Description: </p> 
     * @param custType
     * @return 
     * @see com.betterjr.modules.account.dubbo.interfaces.ICustInfoService#webCoreQueryMoney(java.lang.String) 
     * @author chenlq
     * @date 2018年7月9日 上午11:11:09
     */
    @Override
    public String webCoreQueryMoney(String custType) {
        final Collection<CustInfo> custInfos = custAccountService.coreQueryMoney(custType);
        return AjaxObject.newOk("资金方客户查询成功！", custInfos).toJson();
    }

    /**
     * 资金方查询供应商
     * @param custType
     * @return 
     * @see com.betterjr.modules.account.dubbo.interfaces.ICustInfoService#webMoneyEnterpriseQuerySupplyBussiness(java.lang.String) 
     * @author chenlq
     * @date 2018年7月5日 下午3:59:21
     */
    @Override
    public String webMoneyEnterpriseQuerySupplyBussiness(String custType) {
        final Collection<CustInfo> custInfos = custAccountService.moneyQuerySupply(custType,
                UserUtils.getOperatorInfo().getId(), UserUtils.getOperatorInfo().getOperOrg());
        return AjaxObject.newOk("供应商客户查询成功！", custInfos).toJson();
    }

    /**
     *分页查询平台所有客户信息
     */
    @Override
    public String platformQueryAllCustInfo(String custType, String flag, int pageNum, int pageSize) {
        final Collection<CustInfo> custInfos = custAccountService.queryPlatformAllCust(custType, flag, pageNum,
                pageSize);
        return AjaxObject.newOk("查询所有客户公司成功！", custInfos).toJson();
    }

    @Override
    public Collection<CustInfo> queryCustInfo() {
        final Collection<CustInfo> custInfos = custAccountService
                .findCustInfoByOperator(UserUtils.getOperatorInfo().getId(), UserUtils.getOperatorInfo().getOperOrg());
        return custInfos;
    }

    /***
     * 获取当前登录的客户号
     *
     * @return
     */
    @Override
    public Long findCustNo() {
        final List<Long> custList = custOperatorRelaService.findCustNoList(UserUtils.getOperatorInfo().getId(),
                UserUtils.getOperatorInfo().getOperOrg());
        logger.info("findCustNo custList:" + custList);
        return Collections3.getFirst(custList);
    }

}
